﻿$(document).ready(function () {
    $("#cboState:first").change(function (e) {
        e.preventDefault();
        if ($(this).val() != "") {
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Student/LoadCities",
                data: { idState: $(this).val() },
                success: function (response) {
                    if (response != null && response.length > 0) {
                        $("#cboCity:first option").each(function () {
                            if ($.trim($(this).val()) != "")
                                $(this).remove();
                        });

                        var options = "";
                        for (var i = 0; i < response.length; i++) {
                            var obj = response[i];
                            options += "<option value='" + obj.Id + "' title='" + obj.Name + "'>" + obj.Name + "</option>";
                        }
                        $("#cboCity:first").append(options);
                    }
                },
                error: function (response) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        } else {
            $("#cboCity:first option").each(function () {
                if ($.trim($(this).val()) != "")
                    $(this).remove();
            });
        }
    });

    $("#btnSave:first").click(function (e) {
        e.preventDefault();
        var lblMessage = $("#lblMessage:first").text("");
        $(".required:visible").each(function () {
            $(this).removeClass("ui-state-error");
            if ($(this).val() == "") {
                lblMessage.text("É necessário preencher os campos obrigatórios.");
                $(this).addClass("ui-state-error");
            }
        });

        if ($.trim($("#txtCPF").val()) != "" && !isValidCPF($("#txtCPF").val()) && lblMessage.text() == "") {
            lblMessage.text("Informe um CPF válido.");
            $("#txtCPF").addClass("ui-state-error");
        }

        if (lblMessage.text() == "") {
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Student/Save/" + $("#hdfIdStudent").val(),
                data: {
                    name: $("#txtName").val(),
                    rg: $("#txtRG").val(),
                    cpf: $("#txtCPF").val(),
                    birthdate: $("#txtBirthdate").val(),
                    address: $("#txtAddress").val(),
                    id_city: $("#cboCity option:selected").val(),
                    phone: $("#txtPhone").val(),
                    cellular: $("#txtCellular").val(),
                    id_sex: $("#cboSex option:selected").val(),
                    description: $("#txtDescription").val()
                },
                success: function (json) {
                    document.location.href = "/Student/Index";
                },
                error: function (json) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        };
    });

    $("#txtRG:first").mask("99.999.999-9");
    $("#txtCPF:first").mask("999.999.999-99");
    $("#txtPhone:first").mask("(99) 9999-9999");
    $("#txtCellular:first").mask("(99) 9999-9999");
    $("#txtName:first").focus();
});